import { VIPInfo, updateSiteReserve, siteReserve } from '../../api/index'

Page({

  /* 页面的初始数据 */
  data: {
    id: '',
    price: '',
    date: '',
    list: [],
    hklx: '1',
    show: false,
    pwd: '',
    allPrice: 0,
    siteId: '',
    name: wx.getStorageSync('venueName'),   //场馆名称
    shtel: '',
    shname: '',
    hytel: '',
    hyname: '',
    hyIdCard: '',
    hyid: '',
    remarks: ''
  },
  siteReserve() {
    siteReserve({ param: { siteId: this.data.siteId } }).then(res => {
      this.setData({
        list: res.record.siteList,
        id: res.record.id,
        remarks: res.record.remarks,
        hklx: res.record.userType == '1' ? '2' : '1',
        shtel: res.record.userPhone,
        hytel: res.record.memberPhone,
        hyname: res.record.memberName,
        hyIdCard: res.record.number,
        allPrice: res.record.allPrice,
        hyid: res.record.memberId,
        shname: res.record.userName
      })
    })
  },

  hyClick() {
    if (!this.data.hytel) return wx.showToast({ title: '请输入手机号', icon: 'none', duration: 2000 })
    VIPInfo({
      param: {
        phoneOrNo: this.data.hytel
      }
    }).then(res => {
      if (res.errorCode === '000000') {
        this.setData({ hyname: res.record.nickName, hyIdCard: res.record.number, hyid: res.record.id });
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 关闭密码输入弹框
  onClose() {
    this.setData({ show: false, pwd: '' });
  },

  // 实时获取密码输入框值
  bindKeyInput(e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  // 获取日期
  getDate() {
    let date = new Date();
    let nnn = date.getFullYear()
    let yyy = date.getMonth() + 1
    let rrr = date.getDate()
    let xq = date.getDay()
    let data = ''
    switch (xq) {
      case 1:
        data = '周一'
        break;
      case 2:
        data = '周二'
        break;
      case 3:
        data = '周三'
        break;
      case 4:
        data = '周四'
        break;
      case 5:
        data = '周五'
        break;
      case 6:
        data = '周六'
        break;
      case 0:
        data = '周日'
        break;
    }
    let res = nnn + '年' + yyy + '月' + rrr + '日' + ' ' + data
    this.setData({
      date: res
    })
  },
  // 会员提交订单
  memberTJ() {
    var that = this
    if (!that.data.pwd) return wx.showToast({ title: '请输入支付密码', icon: 'none', duration: 2000 })
    payment({
      userId: wx.getStorageSync('userId'),
      param: {
        memberId: wx.getStorageSync('userId'),
        orderId: that.data.ddId,
        payMethod: that.data.type,
        payPwd: that.data.pwd,
        couponsId: that.data.yhqId
      }
    }).then(res => {
      if (res.errorCode == '000000') {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          success(res) {
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/paymentSuccessful/index'
              })
            }, 2000)
          }
        })
      } else if (res.errorCode == '70012') {
        wx.showToast({
          title: res.errorMsg,
          icon: 'none',
          duration: 2000,
          success(res) {
            that.setData({ pwd: '' })
          }
        })
      } else if (res.errorCode == '00027') {
        wx.showToast({
          title: res.errorMsg,
          icon: 'none',
          duration: 2000,
          success(res) {
            that.setData({ pwd: '', show: false })
          }
        })
      } else {
        wx.showToast({
          title: res.errorMsg,
          icon: 'none',
          duration: 2000,
          success(res) {
            setTimeout(() => {
              wx.reLaunch({
                url: `/pages/paymentFailure/index?ddid=${that.data.ddId}&type=会员支付`
              })
            }, 2000)
          }
        })
      }
    })
  },

  // 提交订单
  submit() {
    var that = this
    if (this.data.hklx == '1') {   //会员
      if (!this.data.hyid) return wx.showToast({ title: '请先选择会员', icon: 'none', duration: 2000 })
      updateSiteReserve({
        param: {
          memberId: this.data.hyid,
          remarks: this.data.remarks,
          reserveId: this.data.id,
          type: 0,
          userName: '',
          userPhone: '',
          userType: 0
        }
      }).then(res => {
        if (res.errorCode == '000000') {
          wx.showToast({
            title: '修改预定成功',
            icon: 'success',
            duration: 2000,
            mask: true,
            success(res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 2000)
            }
          })
        }
      })
    } else {
      if (!this.data.shname) return wx.showToast({ title: '请输入姓名', icon: 'none', duration: 2000 })
      updateSiteReserve({
        param: {
          memberId: '',
          remarks: this.data.remarks,
          reserveId: this.data.id,
          type: 0,
          userName: this.data.shname,
          userPhone: this.data.shtel,
          userType: 1
        }
      }).then(res => {
        if (res.errorCode == '000000') {
          wx.showToast({
            title: '修改预定成功',
            icon: 'success',
            duration: 2000,
            mask: true,
            success(res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 2000)
            }
          })
        }
      })
    }
  },
  onChange(e) {
    this.setData({ hklx: e.detail });
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    this.setData({ siteId: options.siteId })
    this.getDate()
    this.siteReserve()
  },

  /* 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ name: wx.getStorageSync('venueName') })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})